<?php
namespace app\admin\controller;
use app\extra\Rest;

abstract class Base extends Rest
{
    // session 保存的键名
    protected $controller;
    // 区分登陆状态则必须修改这个值
    public static $_login_key = 'admin_user';
    protected $_order = '';
    protected function  initialize()
    {
        $this->controller = request()->controller();
        parent::$_login_key =  self::$_login_key;
    }

    /**
     * 一条数据搜索条件
     * @param $where
     */
    protected function _searchOne(&$where){
        
    }

    /**
     * 查询列表使用的搜索条件
     * @param $where
     */
    protected function _searchPage(&$where){

    }

    /**
     * get获取一条记录
     */
    public function gets()
    {
        $id = input('get.id');
        if(empty($id)){
            $map = [];
            $this->_searchPage($map);
            $this->responsePage($map, $this->_order, $this->controller);
        } else {
            $map['id'] = $id;
            $this->_searchOne($map);
            $this->getOne($map, $this->controller);
        }
    }

    /**
     * post 创建
     */
    public function create(){
        $data = input('post.');
        unset($data['id']);
        $validate = Validate($this->controller);
        if (!$validate->check($data)) {
            $this->responseFail($validate->getError());
        }
        $model = model($this->controller);
        $r = $model->createAdd($data);
        if ($r == false){
            $this->responseFail($model->getError());
        }
        $this->gets();
    }

    /**
     * put修改
     */
    public function update(){
        $data = input('put.');
        if (empty($data['id'])){
            $this->responseFail('id必填');
        }
        $validate = Validate($this->controller);
        if (!$validate->checkRuleByData($data)) {
            $this->responseFail($validate->getError());
        }
        $model = model($this->controller);
        $r = $model->updateSave($data);
        if ($r == false){
            $this->responseFail($model->getError());
        }
        $this->gets();
    }

    /**
     * delete删除
     */
    public function delete(){
        $data = input('delete.');
        if (empty($data['id'])){
            $this->responseFail('id必填');
        }
        $model = model($this->controller);
        $r = $model->deleteData($data['id']);
        if ($r == false){
            $this->responseFail($model->getError());
        }
        $map = [];
        $this->_searchPage($map);
        $model = model($this->controller);
        $count = $model->getListForCount($map);
        $per_page = input('get.size', 20);
        $page = input('get.page', 1);
        $data_list = $model->getListForPage($map, $page, $per_page, $this->_order);
        if ($data_list->isEmpty() && $page != 1){
            $page -= 1;
            $data_list = $model->getListForPage($map, $page, $per_page, $this->_order);
        }
        $this->responseSuccess($data_list, '成功删除', $count, $per_page, $page);
    }
    // 请求数据的方法合集
    /**
     * 方法使用的 用来以分页形式返回数据列表
     * @param array $map
     * @param string $order
     * @param mixed|string $model
     */
    public function responsePage($map=[], $order = '',$model=''){
        if (empty($model)){
            $model = $this->controller;
        }
        $model = model($model);
        $count = $model->getListForCount($map);
        $per_page = input('get.size', 20);
        $page = input('get.page', 1);
        $data_list = $model->getListForPage($map, $page, $per_page, $order);
        $this->responseSuccess($data_list, '查询成功', $count, $per_page);
    }

    /**
     * 获取一条数据
     * @param $id
     * @param string $model
     */
    public function getOne($id,$model=''){
        if (empty($model)){
            $model = $this->controller;
        }
        $model = model($model);
        $data = $model->getOne($id);
        $this->responseSuccess($data);
    }
}